<template>
  <form-dialog
    :title="$t('common.edit')"
    :visible="visible"
    width="30%"
    @handleClose="handleClose"
  >
    <template #content>
      <div class="tip-container">
        <el-form ref="form" :model="formData" :rules="formRules" v-loading="loading" label-width="122px">
          <el-form-item :label="$t('workspace.desktopQuota')" prop="desktopQuota" v-if="composite_specification === '0'">
            <el-input type="number" :min="minDesktopNumber" v-model.number="formData.desktopQuota" :title="''" ></el-input>
          </el-form-item>
          <div style="height: 18px;"></div>
          <el-form-item :label="$t('common.description')" prop="description">
             <el-input v-model="formData.description" type="textarea" maxlength="200" show-word-limit></el-input>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <template #footer>
      <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click.native="submit" :loading="loading">{{ $t('common.confirm') }}</el-button>
    </template>
  </form-dialog>
</template>
<script lang="ts">
import Vue from 'vue';
import FormDialog from '../../../components/Dialog/formDialog.vue';
export default Vue.extend({
  components: { FormDialog },
  props: {
    updateMethod: {
      type: Function,
      default() {
        return () => Promise.resolve();
      }
    }
  },
  data() {
		const validateEditDesktopQuota = (rule: any,value: any, callback: any) => {
      if (this.composite_specification === "1") return callback();
			if((value < this.minDesktopNumber)) {
				return callback (new Error( (this as any).$t(`workspace.desktopQuotaTips`)));
			}
			return callback();
		};
    return {
      visible: false,
      loading: false,
      formData: {
        desktopQuota: 0,
        description: '',
        id: "",
      },
			minDesktopNumber: 0,
			project_type: '',
      composite_specification: '',
      formRules: {
				desktopQuota: [
					{validator: validateEditDesktopQuota, tigger: 'blur'},
					{validator: (rule: any,value: any,callback: any) => {
            if (this.composite_specification === "1") return callback();
            if(value) {
              if(/\D/g.test(value)) {
                callback(new Error((this as any).$t('workspace.desktopQuotaErrorTip')));
              } else {
                callback();
              }
            }
          }, tigger: 'blur'},
				],
				description: [
          { max: 200, message: this.$t('policy.message.descriptionLength'), trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    init(formData: any) {
      (this as any).formData = {
        desktopQuota: 0,
        description: '',
        id: "",
      }
      this.visible = true;
			this.formData.id = formData.id
			this.formData.desktopQuota = formData.desktop_quota;
			this.minDesktopNumber = formData.desktop_count + 1;
			this.formData.description = formData.description;
      this.project_type = formData.project_type;
      this.composite_specification = formData.composite_specification;
			// this.$pandaApi.apiGetSystemSettings().then((res: any) => {
			// 	if(this.minDesktopNumber >= res.desktop_quota_of_horizon_edge) {
			// 		this.maxDesktopNumber = this.minDesktopNumber
			// 	} else {
			// 		this.maxDesktopNumber = res.desktop_quota_of_horizon_edge
			// 	}
			// })
    },
    handleClose() {
      (this as any).visible = false;
      (this as any).loading = false;
			if (this.$refs['form']) {
				(this.$refs['form'] as any).clearValidate();
			}
    },
    submit() {
			(this.$refs.form as any).validate((valid: any) => {
				if (valid) {
					this.loading = true;
					let obj: any = {};
					obj.description = this.formData.description;
					obj.id = this.formData.id;
					if (this.composite_specification !== "1") {
						obj.desktop_quota = this.formData.desktopQuota;
					}
					this.$pandaApi.apiEditWorkSpace(obj).then((res: any) => {
						if(res.code === 2000) {
							(this as any).$message.success((this as any).$t('markdowns.editSuccess'));
							(this as any).$emit("success");
							(this as any).handleClose();
						} else {
							(this as any).$message.warning((this as any).$t('markdowns.editFail'));
						}
					}).catch((err: any) => {
						(this as any).$message.warning((this as any).$t('markdowns.editFail'));
						this.loading = false;
					}).finally(() => {
						this.loading = false;
					})
				}
   		})
    },
  }
})
</script>
<style lang="scss" scoped>
::v-deep .el-form {
  display: flex;
  flex-direction: column;
  align-items: center;

  .el-form-item.el-form-item--small {
    display: flex;
    justify-content: flex-end !important;
    align-items: center;
    padding-right: 50px;
    width: 88%;
    flex-shrink: 0;
  }
}
</style>
